package com.android.gpuimage;

import android.graphics.Bitmap;

import pl.droidsonroids.gif.GifBitmap;

public class GPUImageGifFilter
        extends GPUImageDynamicFilter {

    private boolean mPreviewMode = false;
    GifBitmap mGifBitmap;

    public GPUImageGifFilter(GifBitmap bitmap) {
        super();
        mGifBitmap = bitmap;
    }

    /**
     * 是否预览模式，是预览模式则gif图不断更新，非预览模式则根据{@link #updateTimestamp(long)}绘制特定的帧，默认为false
     * @param previewMode
     */
    public void setPreviewMode(boolean previewMode) {
        mPreviewMode = previewMode;
    }

    @Override
    protected Bitmap getCurrentBitmap(long timestamp) {
        if (mPreviewMode) {
            if (!mGifBitmap.isPlaying()) {
                mGifBitmap.start();
            }
            return mGifBitmap.getCurrentFrame();
        } else {
            if (mGifBitmap.isPlaying()) {
                mGifBitmap.stop();
            }
            return mGifBitmap.seekToPositionAndGet((int) (timestamp % mGifBitmap.getDuration()));
        }
    }



    public void onDestroy() {
        super.onDestroy();
        mGifBitmap.stop();
    }
}
